#!/bin/bash

OFILE=$1
IFS=$(echo -en "\n\b")

for DIR in "titan-phase-results-128"
do
 for FILE in $DIR/*.txt #astar-64.txt
 do
 
  BENCH_NAME=$(echo $FILE | cut -d"/" -f2 | cut -d"-" -f1)
  echo $BENCH_NAME
  Total_sim_cycle=( $(grep "Total simulation cycle" $FILE | cut -d":" -f2 | cut -d" " -f2) ) 
  #IPC=( $(grep "fp:" $FILE | cut -d":" -f2 | cut -d"i" -f1) )
  #ipc=( $(grep "fp:" $FILE | cut -d":" -f3 | cut -d"i" -f1) )
  #l1mpki=( $(grep "fp:" $FILE | cut -d":" -f5 | cut -c 1-5 ) )
  #l2mpki=( $(grep "fp:" $FILE | cut -d":" -f6 | cut -c 1-5) )
  #Avg_Mesg_Lat=( $(grep "Avg. Message Latency" $FILE | cut -d":" -f2 | cut -d"c" -f1) ) 
 
  #for i in {0..300}
  #do
  # echo $BENCH_NAME ${Total_sim_cycle[i]} ${IPC[i]} ${ipc[i]} ${l1mpki[i]} ${l2mpki[i]} ${Avg_Mesg_Lat[i]} >> $OFILE.$BENCH_NAME.res
  #done
  
  array_num_net_phase_this_ph=()
  array_tot_net_length=()
 
  for j in {0..300} #{0..5} #$((${#Total_sim_cycle[@]}))
   do
    #COP=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 15 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    #array_cop=( ${array_cop[@]} $COP)
    #COMP=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 16 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    #array_comp=( ${array_comp[@]} $COMP)
    #NUM_NET_PH=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 17 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    #array_num_net_phase=( ${array_num_net_phase[@]} $NUM_NET_PH)
    #RUNNING_AVG_HT_PER_NET_PH=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 23 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    #array_running_avg_ht_per_net_phase=( ${array_running_avg_ht_per_net_phase[@]} $RUNNING_AVG_HT_PER_NET_PH)
    NUM_NET_PH_THIS_PH=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 22 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    array_num_net_phase_this_ph=( ${array_num_net_phase_this_ph[@]} $NUM_NET_PH_THIS_PH)
    #PHS_RUNNING_AVG_HT_PER_NET_LN=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 24 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    #array_phs_running_avg_ht_per_net_phase=( ${array_phs_running_avg_ht_per_net_phase[@]} $PHS_RUNNING_AVG_HT_PER_NET_LN)
    TOT_NET_LENGTH=$(grep "@clock:"${Total_sim_cycle[j]} $FILE | grep COP | cut -d":" -f 18 | cut -d" " -f1 | awk '{sum+=$1} END {print sum/NR}')
    array_tot_net_length=( ${array_tot_net_length[@]} $TOT_NET_LENGTH)
    #echo ${Total_sim_cycle[j]} ${array_cop[j]}
    echo $BENCH_NAME $j #"1." $NUM_NET_PH "2." $RUNNING_AVG_HT_PER_NET_PH "3." $NUM_NET_PH_THIS_PH "4." $PHS_RUNNING_AVG_HT_PER_NET_LN
  done
  
  #echo $BENCH_NAME "Tot_CYC IPC ipc l1mpki l2mpki Mesg_Lat COP COMP Num_NET_PH RUN_AVG_HT_PER_LN NUM_NET_PH_TH_PH PH_RUN_AVG_HT_PER_NET_LN" >> $OFILE.$BENCH_NAME.res
  echo $BENCH_NAME "Tot_CYC NUM_NET_PH_TH_PH TOT_NET_LENGTH" >> $OFILE.$BENCH_NAME.res
  for i in {0..300} #{0..5}
  do
   echo ${Total_sim_cycle[i]} ${array_num_net_phase_this_ph[i]} ${array_tot_net_length[i]}>> $OFILE.$BENCH_NAME.res
  done
 
 #echo ${array_cop[@]}

 done
done

#for j in {0..300}
#do
# echo $COP
#done

#COP=( $(grep ${Total_sim_cycle[j]} $FILE | grep "@clock" | grep COP | cut -d":" -f 15 | cut -d" " -f1 | awk -F' ' '{sum=0; for(p=0; p<=NF; p++){sum+=$p}; print "COP:", sum/NF}') )
